#!/bin/bash
PTAH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

. /etc/os-release
dir=/nginx/wp
dir1=/nginx/she
ip=172.16.1.0/24

color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "    
    elif [ $2 = "failure" -o $2 = "1"  ] ;then 
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo 
}

config_nfs(){
if [[ $ID =~ centos|rocky ]];then
		yum -y install nfs-utils &> /dev/null 
		cat > /etc/exports <<EOF
$dir	$ip(rw,all_squash,anonuid=666,anongid=666) 
$dir1	$ip(rw,all_squash,anonuid=666,anongid=666) 
EOF

	id 666 &> /dev/null
	if [[ !  $? -eq 0 ]];then
		groupadd -g666 www;useradd -u 666 -g 666 -M -s /sbin/nologin www 
	fi
	[[ -d $dir ]] || mkdir -p $dir;chown -R www.www $dir	
	[[ -d $dir1 ]] || mkdir -p $dir1;chown -R www.www $dir1
else
	echo -e "\E[1;41m 不支持该系统 \E[0m"

fi	
}

start_file(){
	systemctl start nfs	
	systemctl enable nfs
	systemctl restart nfs
	systemctl is-active nfs
	if [[ $? -eq 0 ]];then
		color "nfs服务配置成功" 0
	else 
		color "nfs服务配置失败" 1
		exit 1
	fi


}
config_nfs
start_file
